//
//  SubAirSurfaceView.m
//  Air3DViewDemo
//
//  Created by yanghao on 15/11/2017.
//  Copyright © 2017 simu-tech. All rights reserved.
//

#import "SubAirSurfaceView.h"

@implementation SubAirSurfaceView

static SubAirSurfaceView *_instance;
+(instancetype)shareInstance:(NSString *)xfileName view:(UIView *)view
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [[SubAirSurfaceView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];

            
            
            
        }

    });
    
    // 显示摇杆
    [_instance showStick];
    // 显示工具栏
    [_instance showToolsBar];
    [view addSubview:_instance];
    // 设置点击模型的回调
    [_instance.airSurfaceRender setSelectedBlock:^(AirSegmentKey *airSegmentKey) {
        
    }];
    NSString * guid = xfileName;
    NSString * file = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:guid];
    NSLog(@"guid = %@",guid);
    NSLog(@"file = %@",file);
    //文件转换
    const char* path = [file cStringUsingEncoding:NSASCIIStringEncoding];
    const char* fileName = [file cStringUsingEncoding:NSASCIIStringEncoding];
    int result = [AirSurfaceRender convertBigGDfile:fileName tempPath:path];
    NSLog(@"%d",result);
    
    NSString *newFile = [[NSString alloc]initWithCString:path encoding:NSASCIIStringEncoding];
    NSLog(@"%@",newFile);
    BOOL isModelImported = [[[_instance airSurfaceRender] loadedGuidArray] containsObject:[guid stringByDeletingPathExtension]];
    UIActivityIndicatorView * activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    activityIndicatorView.center=view.center;
    [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicatorView setBackgroundColor:[UIColor blackColor]];
    [view addSubview:activityIndicatorView];
    [activityIndicatorView startAnimating];
    if (isModelImported) {
        [_instance.airSurfaceRender unloadModelFile:[guid stringByDeletingPathExtension] completion:^(BOOL finished) {
            if (finished) {
                NSLog(@"unloadModelFile OK");
                [activityIndicatorView stopAnimating];
                
                //            [[airSurfaceView airSurfaceRender] setViewMode:ViewModeIosmetric];
            } else {
                NSLog(@"unloadModelFile error");
            }
        }];
    }else{
        [_instance.airSurfaceRender loadModelFile:newFile waitUntilDone:YES completion:^(BOOL finished) {
            if (finished) {
                NSLog(@"loadModelFile OK");
                [activityIndicatorView stopAnimating];
                
                //                [[airSurfaceView airSurfaceRender] setViewMode:ViewModeIosmetric];
            } else {
                NSLog(@"loadModelFile error");
            }
        }];
    }
    return _instance;
}


@end
